package com.dawn.module.cms.service.doctor;

import cn.hutool.core.collection.CollUtil;
import com.dawn.framework.common.util.collection.MapUtils;
import com.dawn.module.cms.dal.dataobject.department.DepartmentDO;
import com.dawn.module.cms.service.department.DepartmentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.dawn.module.cms.controller.admin.doctor.vo.*;
import com.dawn.module.cms.dal.dataobject.doctor.DoctorDO;
import com.dawn.framework.common.pojo.PageResult;
import com.dawn.framework.common.pojo.PageParam;
import com.dawn.framework.common.util.object.BeanUtils;

import com.dawn.module.cms.dal.mysql.doctor.DoctorMapper;

import static com.dawn.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.dawn.framework.common.util.collection.CollectionUtils.convertSet;
import static com.dawn.module.cms.enums.ErrorCodeConstants.*;

/**
 * 医生信息 Service 实现类
 *
 * @author root
 */
@Service
@Validated
public class DoctorServiceImpl implements DoctorService {

    @Resource
    private DoctorMapper doctorMapper;

    @Resource
    private DepartmentService departmentService;

    @Override
    public Long createDoctor(DoctorSaveReqVO createReqVO) {
        // 插入
        DoctorDO doctor = BeanUtils.toBean(createReqVO, DoctorDO.class);
        doctorMapper.insert(doctor);
        // 返回
        return doctor.getId();
    }

    @Override
    public void updateDoctor(DoctorSaveReqVO updateReqVO) {
        // 校验存在
        validateDoctorExists(updateReqVO.getId());
        // 更新
        DoctorDO updateObj = BeanUtils.toBean(updateReqVO, DoctorDO.class);
        doctorMapper.updateById(updateObj);
    }

    @Override
    public void deleteDoctor(Long id) {
        // 校验存在
        validateDoctorExists(id);
        // 删除
        doctorMapper.deleteById(id);
    }

    private void validateDoctorExists(Long id) {
        if (doctorMapper.selectById(id) == null) {
            throw exception(DOCTOR_NOT_EXISTS);
        }
    }

    @Override
    public DoctorDO getDoctor(Long id) {
        return doctorMapper.selectById(id);
    }

    @Override
    public PageResult<DoctorRespVO> getDoctorPage(DoctorPageReqVO pageReqVO) {
        PageResult<DoctorDO> pageResult = doctorMapper.selectPage(pageReqVO);
        return new PageResult<>(buildProductVOList(pageResult.getList()), pageResult.getTotal());
    }

    private List<DoctorRespVO> buildProductVOList(List<DoctorDO> list) {
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        Map<Long, DepartmentDO> deptMap = departmentService.getDepartmentMap(
                convertSet(list, DoctorDO::getDepartmentId));
        return BeanUtils.toBean(list, DoctorRespVO.class, doctor -> {
            MapUtils.findAndThen(deptMap, doctor.getDepartmentId(),
                    dept -> doctor.setDepartmentName(dept.getName())
                            .setHisDeptCode(dept.getHisDeptCode())
                            .setHisDoctorName(dept.getHisDeptName())
                            .setAddress(dept.getAddress()));
        });
    }

}